<?xml version="1.0" encoding="Windows-1251"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" version="1.0" encoding="Windows-1251" media-type="text/xhtml"/>

    <xsl:strip-space elements="*"/>

    <xsl:template match="/">
        <html>
            <head>
                <title>
                    <xsl:text>XML Generation</xsl:text>
                </title>
                <link rel="stylesheet" type="text/css" href="css/db_internal_model.css"/>
            </head>
            <body>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="relation">
        <table cellpadding="0" cellspacing="0">
            <tbody>
                <tr>
                    <th><xsl:value-of select="@name"/></th>
                </tr>
                <xsl:apply-templates select="keys/primary"/>
                <xsl:apply-templates select="attribute"/>
            </tbody>
        </table>
    </xsl:template>

    <xsl:template match="primary/key/attribute">
        <xsl:variable name="name" select="@name"/>
        <xsl:variable name="attribute" select="ancestor::relation/attribute[@name = $name]"/>
        <tr>
            <xsl:apply-templates select="." mode="css-class"/>
            <td>
                <xsl:value-of select="$name"/>
                <xsl:if test="@foreign">
                    <xsl:text>&#160;(FK)</xsl:text>
                </xsl:if>
            </td>
        </tr>
    </xsl:template>

    <xsl:template match="relation/attribute">
        <xsl:variable name="name" select="@name"/>
        <xsl:if test="not(../keys//attribute[@name = $name])">
            <tr>
                <xsl:apply-templates select="." mode="css-class"/>
                <td>
                    <xsl:value-of select="@name"/>
                </td>
            </tr>
        </xsl:if>
    </xsl:template>

    <xsl:template match="primary/key/attribute[1]" mode="css-class">
        <xsl:attribute name="class">
                <xsl:text>first-primary</xsl:text>
        </xsl:attribute>
    </xsl:template>

    <xsl:template match="primary/key/attribute[position() > last()]" mode="css-class">
        <xsl:attribute name="class">
                <xsl:text>primary</xsl:text>
        </xsl:attribute>
    </xsl:template>

    <xsl:template match="primary/key/attribute[position() = last()]" mode="css-class">
        <xsl:attribute name="class">
                <xsl:text>last-primary</xsl:text>
        </xsl:attribute>
    </xsl:template>

    <xsl:template match="primary/key/attribute[last() = 1]" mode="css-class">
        <xsl:attribute name="class">
                <xsl:text>last-primary first-primary</xsl:text>
        </xsl:attribute>
    </xsl:template>

</xsl:stylesheet>